package com.b511.swpu.cementbackend.util.convert;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

@Setter
@Getter
//实现类
public abstract class Context {

    private Double value;

    private Convert forwardConvert;

    private Convert backwardConvert;

    public Context setValue(Double value) {
        this.value = value;
        return this;
    }

    public Context setForwardConvert(String forwardUnitName) {
        this.forwardConvert = StringUtils.hasText(forwardUnitName) ? createConvert(forwardUnitName) : null;
        return this;

    }

    public Context setBackwardConvert(String backwardUnitName) {
        this.backwardConvert = StringUtils.hasText(backwardUnitName) ? createConvert(backwardUnitName) : null;
        return this;
    }

    protected abstract Convert createConvert(String unitName);

    public final Double getValue() {
        if (backwardConvert != null) {
            value = backwardConvert.backward(value);
        }
        if (forwardConvert != null) {
            value = forwardConvert.forward(value);
        }
        return value;
    }

}
